package io.jt808.proto.req;

import io.norland.annotations.Proto;
import io.norland.buffer.AutoBuffer;
import io.norland.proto.IProto;
import lombok.Data;

/**
 * 查询终端属性应答
 */
@Proto("0107")
@Data
public class JT0107 implements IProto {

    /**
     * 终端类型
     */

    private short terminalType;
    /**
     * 制造商ID
     */
    private String manufactureId;
    /**
     * 终端型号
     */
    private String terminalModelNo;
    /**
     * 终端ID
     */
    private String terminalId;
    /**
     * 终端sim卡ICCID
     */
    private String iccid;
    /**
     * 终端硬件版本号长度
     */
    private int hardwareVersionLen;
    /**
     * 终端硬件版本号
     */
    private String hardwareVersion;
    /**
     * 终端固件版本号长度
     */
    private int firmwareVersionLen;
    /**
     * 终端固件版本号
     */
    private String firmwareVersion;
    /**
     * gnss模块属性
     */
    private byte gnssAttr;
    /**
     * 通信模块属性
     */
    private byte communicationAttr;

    public final byte[] writeToBytes() {
        AutoBuffer buff = new AutoBuffer();
        buff.writeShort(terminalType);
        buff.writeHexString(manufactureId, 5);
        buff.writeHexString(terminalModelNo, 20);
        buff.writeHexString(terminalId, 7);
        buff.writeBcdString(iccid, 10);
        buff.writeByte((byte) hardwareVersionLen);
        buff.writeString(hardwareVersion);
        buff.writeByte((byte) firmwareVersionLen);
        buff.writeString(firmwareVersion);
        buff.writeByte(gnssAttr);
        buff.writeByte(communicationAttr);
        return buff.array();
    }

    public final void readFromBuf(AutoBuffer buff) {
        terminalType = buff.readShort();
        manufactureId = buff.readHexString(5);
        terminalModelNo = buff.readHexString(20);
        terminalId = buff.readHexString(7);
        iccid = buff.readBcdString(10);
        hardwareVersionLen = buff.readByte();
        hardwareVersion = buff.readString(hardwareVersionLen);
        firmwareVersionLen = buff.readByte();
        firmwareVersion = buff.readString(firmwareVersionLen);
        gnssAttr = buff.readByte();
        communicationAttr = buff.readByte();
    }

    /**
     * 是否适用客运车辆
     */
    public boolean isSuitableForPassengerTransport() {
        return (terminalType & 1) != 0;
    }

    /**
     * 是否适用危险品车辆
     */
    public boolean isSuitableForDangerousGoods() {
        return (terminalType & 1 << 1) != 0;
    }

    /**
     * 是否适用普通货运车辆
     */
    public boolean isSuitableForGeneralCargo() {
        return (terminalType & 1 << 2) != 0;
    }

    /**
     * 是否适用出租车辆
     */
    public boolean isTaxi() {
        return (terminalType & 1 << 3) != 0;
    }

    /**
     * 是否支持硬盘录像
     */
    public boolean isSupportHardDiskAndRecordVideo() {
        return (terminalType & 1 << 6) != 0;
    }

    /**
     * true：一体机，false：分体机。
     */
    public boolean isIntegratedMachine() {
        return (terminalType & 1 << 7) == 0;
    }

    /**
     * 是否支持GPS
     */
    public boolean isSupportGPS() {
        return (gnssAttr & 1) != 0;
    }

    /**
     * 是否支持BeiDou
     */
    public boolean isSupportBeiDou() {
        return (gnssAttr & 1 << 1) != 0;
    }

    /**
     * 是否支持 GLONASS 定位
     */
    public boolean isSupportGLONASS() {
        return (gnssAttr & 1 << 2) != 0;
    }

    /**
     * 是否支持 Galileo 定位
     */
    public boolean isSupportGalileo() {
        return (gnssAttr & 1 << 3) != 0;
    }

    /**
     * 是否支持 GPRS通信
     */
    public boolean isSupportGPRS() {
        return (communicationAttr & 1) != 0;
    }

    /**
     * 是否支持CDMA通信
     */
    public boolean isSupportCDMA() {
        return (communicationAttr & 1 << 1) != 0;
    }

    /**
     * 是否支持TD-SCDMA通信
     */
    public boolean isSupportTDSCDMA() {
        return (communicationAttr & 1 << 2) != 0;
    }

    /**
     * 是否支持WCDMA通信
     */
    public boolean isSupportWCDMA() {
        return (communicationAttr & 1 << 3) != 0;
    }

    /**
     * 是否支持CDMA2000通信
     */
    public boolean isSupportCDMA2000() {
        return (communicationAttr & 1 << 4) != 0;
    }

    /**
     * 是否支持TD-LTE通信
     */
    public boolean isSupportTDLTE() {
        return (communicationAttr & 1 << 5) != 0;
    }

    /**
     * 是否支持其他通信方式
     */
    public boolean isSupportSomeOthers() {
        return (communicationAttr & 1 << 7) != 0;
    }


//    public static void main(String[] args) {
//        byte[] bytes = new byte[]{
//                (byte) 0xFF, (byte) 0xFF,
//                0x41, 0x41, 0x41, 0x41, 0x41,
//                0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
//                0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
//                0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41,
//                0x02,
//                0x00, 0x31,
//                0x02,
//                0x00, 0x31,
//                (byte) 0xFF,
//                (byte) 0xFF
//        };
//        AutoBuffer buffer = new AutoBuffer(bytes);
//        JT0107 jt0107 = new JT0107();
//        jt0107.readFromBuf(buffer);
//        System.out.println(jt0107.isSuitableForPassengerTransport());
//        System.out.println(jt0107.isSuitableForDangerousGoods());
//        System.out.println(jt0107.isSuitableForGeneralCargo());
//        System.out.println(jt0107.isSupportHardDiskAndRecordVideo());
//        System.out.println(jt0107.isTaxi());
//        System.out.println(jt0107.isIntegratedMachine());
//        System.out.println(jt0107.isSupportGPS());
//        System.out.println(jt0107.isSupportBeiDou());
//        System.out.println(jt0107.isSupportGLONASS());
//        System.out.println(jt0107.isSupportGalileo());
//        System.out.println(jt0107.isSupportGPRS());
//        System.out.println(jt0107.isSupportCDMA());
//        System.out.println(jt0107.isSupportTDSCDMA());
//        System.out.println(jt0107.isSupportWCDMA());
//        System.out.println(jt0107.isSupportCDMA2000());
//        System.out.println(jt0107.isSupportTDLTE());
//        System.out.println(jt0107.isSupportSomeOthers());
////        System.out.println(new String(bytes).equals(new String(jt0107.writeToBytes())));
//    }
}